#extractAdcircSubset.R
#
# Last modified 13 August 2008
# Author: Cameron Bracken
#         Student Researcher
#         NOAA National Weather Service
#         300 Startare Drive
#         Eureka, CA 95501
#         cameron.bracken@noaa.gov
#
#
#
#This function takes in a vector of adcirc timestamps
#and gives bak a list containing a subset of adcirc data which 
#corresponds to the adcirc data.
#
#this function looks for 
#   fort.64.chopped 
#   fort.64.times 
#   nodes
#Since these files are creates by ppCurrents this fuinction 
#REQUIRES THAT ppCurrents be run prior to calling 
#
#

extractAdcircSubset = function(swantimes, thin = 10, chopped = 'chopped/'){
    
    library(sfsmisc)
    library(matlab)
    
    chopfile = paste(chopped,'fort.64.chop',sep='')
    timefile = paste(chopped,'fort.64.times',sep='')
    nodefile = paste(chopped,'fort.14.chop',sep='')

    parms = scan(chopfile, nlines = 1, skip = 1)
    nsets = parms[1]
    nnodes = parms[2]

    xy = matrix(scan(nodefile, nlines = nnodes),ncol=3,byrow=T)
    adcirctimes = matrix(scan(timefile),ncol=2,byrow=T)[,1]
    uv = matrix(scan(chopfile, skip = 2),ncol=3,byrow=T)[,2:3]
    uv = array(uv,c(nrow(xy),2,length(adcirctimes)))

 
        #this little dude is the thinning vector (skip-sequence)
    skseq = seq(1,nnodes,by=thin)
   
    xy = xy[skseq,]
    uv = uv[skseq, , adcirctimes %in% swantimes]
 
    return(list(xy=xy,uv=uv))
}    
